﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Sinacor.OMM.ConnectionManager.MSMQ;
using Sinacor.OMM.DumpEntity;
using System.Messaging;
using Sinacor.OMM.DumpConfiguration;
using System.Text.RegularExpressions;

namespace Sinacor.OMM.DumpClient
{
    public class Dump : IDisposable
    {

        private static Dump _dumpClient;
        private static Mutex _mutex;
        private static MsmqSender _msmqSender;

        private Regex regex = new Regex("[^a-zA-Z0-9]");

        private Dump()
        {
            _msmqSender = new MSMQHelper().CreateRepository(DumpConfigurationManager.DumperConfiguration.MessageRepository);
        }

        public static Dump GetInstance()
        {
            if (_mutex == null)
                _mutex = new Mutex();

            _mutex.WaitOne();

            if (_dumpClient == null)
                _dumpClient = new Dump();

            _mutex.ReleaseMutex();

            return _dumpClient;
        }

        public bool Send(DumpLineData dumpLineData)
        {
            bool ret = false;
            if (ValidaDadosBasicos(dumpLineData.FolderName, dumpLineData.FileName))
            {
                if (_msmqSender != null)
                {
                    lock (_msmqSender)
                    {
                        Message msg = new Message();
                        msg.Label = DumpType.ConvertToInt(dumpLineData.Type).ToString();
                        msg.Body = dumpLineData;

                        ret = _msmqSender.Send(msg);
                        LastErrorCode = _msmqSender.LastErrorCode;
                        LastErrorMessage = _msmqSender.LastErrorMessage;
                    }
                }
            }
            return ret;
        }

        public bool Send(DumpTraceData dumpTraceData)
        {
            bool ret = false;

            if (ValidaDadosBasicos(dumpTraceData.FolderName, dumpTraceData.FileName))
            {
                if (_msmqSender != null)
                {
                    lock (_msmqSender)
                    {
                        Message msg = new Message();
                        msg.Label = DumpType.ConvertToInt(dumpTraceData.Type).ToString();
                        msg.Body = dumpTraceData;

                        ret = _msmqSender.Send(msg);
                        LastErrorCode = _msmqSender.LastErrorCode;
                        LastErrorMessage = _msmqSender.LastErrorMessage;
                    }
                }
            }
            return ret;
        }

        public bool Send(DumpStreamData dumpStreamData)
        {
            bool ret = false;

            if (ValidaDadosBasicos(dumpStreamData.FolderName, dumpStreamData.FileName))
            {
                if (_msmqSender != null)
                {
                    lock (_msmqSender)
                    {
                        Message msg = new Message();
                        msg.Label = DumpType.ConvertToInt(dumpStreamData.Type).ToString();
                        msg.Body = dumpStreamData;

                        ret = _msmqSender.Send(msg);
                        LastErrorCode = _msmqSender.LastErrorCode;
                        LastErrorMessage = _msmqSender.LastErrorMessage;
                    }
                }
            }
            return ret;
        }

        public bool Send(DumpErrorData dumpErrorData)
        {
            bool ret = false;

            DumpTraceData dld = new DumpTraceData();
            dld.FolderName = "Erros";
            dld.FolderName = "Mensagens com erro";
            dld.DataToDump = dumpErrorData.ToString();

            if (ValidaDadosBasicos(dld.FolderName, dld.FileName))
            {
                if (_msmqSender != null)
                {
                    lock (_msmqSender)
                    {
                        Message msg = new Message();
                        msg.Label = DumpType.ConvertToInt(dld.Type).ToString();
                        msg.Body = dld;

                        ret = _msmqSender.Send(msg);
                        LastErrorCode = _msmqSender.LastErrorCode;
                        LastErrorMessage = _msmqSender.LastErrorMessage;
                    }
                }
            }
            
            return ret;
        }

        public bool ValidaDadosBasicos(string foldername, string filename)
        {

            if (foldername.Trim().Length <= 0)
                return false;

            if (regex.Match(foldername).Success)
                return false;

            if (filename.Trim().Length <= 0)
                return false;

            if (regex.Match(filename).Success)
                return false;

            return true;
        }

        private string _lastErrorMessage;
        public string LastErrorMessage
        {
            get { return _lastErrorMessage; }
            set { _lastErrorMessage = value; }
        }

        private int _lastErrorCode;
        public int LastErrorCode
        {
            get { return _lastErrorCode; }
            set { _lastErrorCode = value; }
        }

        public void Dispose()
        {
            if (_msmqSender != null)
            {
                _msmqSender.Dispose();
                _msmqSender = null;
            }

            if (_mutex != null)
            {
                _mutex.Close();
                _mutex = null;
            }

            if (_dumpClient != null)
            {
                _dumpClient = null;
            }
        }
    }
}
